package StudentService;

import Student.Student;
import StudentDao.StudentDao;
import StudentDao.StudentDaoInterface;
import exception.LoginExcepiton;

import java.util.Random;

public class StudentService  implements StudentServiceInterface {
    StudentDaoInterface studentDao = new StudentDao();

    public void zhuce(Student student) {
        studentDao.zhuce(student);
    }

    @Override
    public boolean Exit(String name) {
        boolean flag = studentDao.Exit(name);
        return flag;
    }

    public void findAllStudent() {
        studentDao.findAllStudent();
    }

    @Override
    public StringBuilder yanzhengma() {
        Random r = new Random();
        char[] chars = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '0', '2', '3', '4', '5', '6', '7', '8', '9', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'};
        StringBuilder b = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int c = r.nextInt(chars.length - 1);
            b.append(chars[c]);
        }
        return b;
    }

    @Override
    public void login(Student student) {
        Student student1 = studentDao.findStudent();
        if (student.getName().equals(student1.getName()) && student.getPassword().equals(student1.getPassword())) {
            System.out.println("登入成功");
        } else {
            throw new LoginExcepiton("登入失败");
        }
    }

    @Override
    public boolean updatePassWord(String name, String newPassWord) {
        boolean flag = studentDao.updatePassWord(name, newPassWord);
        return flag;
    }

    @Override
    public boolean yanzhen(StringBuilder yanzhengma, String yanzhen) {
        String s = yanzhengma.toString();
        boolean fl =false;
        if (yanzhen.equalsIgnoreCase(s)){
            fl = true;
        }
        return fl;
    }


}


